﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DBShell
{
    class CmdOrganizer
    {
        private Queue<string> cmds;
        private Queue<string> buffers;
        private static int count = 0;
        private string[] reservedCmd = { "run", "cls", "exit", "import","execute","usedb","closedb" };
        private TextWriter sw;

        public CmdOrganizer(TextWriter sw)
        {
            this.sw = sw;
            cmds = new Queue<string>();
            buffers = new Queue<string>();
        }


        private string formCmd()
        {
            string cmd = "";
            while (buffers.Count > 0)
            {
                cmd += buffers.Dequeue();
                cmd += ' ';
            }
            cmd = Util.ReplaceSpace(cmd);
            //cmd = Util.Standazation(cmd);
            return cmd.Trim();

        }
        public bool addBuffer(string buffer)
        {
            count++;
            if (reservedCmd.Contains(buffer))
            {
                Program.RunSystemCmd(buffer, this, sw);
                return true;
            }

            if (buffer.Length > 0)
            {
                buffers.Enqueue(buffer);
            }
            else
            {
                string a = formCmd();
                if (a.Length > 0)
                    cmds.Enqueue(a);
            }
            return true;
        }

        public string GetOneCmd()
        {
            if (cmds.Count > 0)
            {
                return cmds.Dequeue();
            }
            return "";
        }

    }
}
